import { _decorator, Component, Node, UITransform, ScrollView, v3, Vec2, Vec3, v2, Size } from 'cc';
import { DEV } from 'cc/env';
import { LayoutViewHelper } from './LayoutViewHelper';
import { SuperButton } from '../base/SuperButton';
const { ccclass, property } = _decorator;
/**
 * 使用坐标移动子节点
 */
let _tempVec2 = new Vec2();
let _temp = new Vec2();
@ccclass('ScrollPosViewHelper')
export class ScrollPosViewHelper extends LayoutViewHelper {

    @property({ tooltip: DEV && "是否根据content每一项的坐标走" })
    @property
    followItem: boolean = false;//

    @property(ScrollView)
    scroll: ScrollView = null;

    @property
    isLimit: boolean = true

    start() {
        let off = this.scroll.getScrollOffset()
        // LogUtils.log(' off ', off)
    }

    public onItemSelectedCallback(itemView: SuperButton, select: boolean) {
        super.onItemSelectedCallback(itemView, select)
        this.scrollMove(itemView, this.isLimit)
    }

    public scrollMove(itemView: SuperButton, isLimit: boolean = true) {
        if (this.followItem) {
            this.scrollFollowItem(itemView, isLimit)
        } else {
            this.scrollNotFollowItem(itemView, isLimit)
        }
    }

    protected scrollNotFollowItem(itemView: SuperButton, isLimit: boolean = true) {
        let wpos = itemView.node.worldPosition;
        let itemPos = v3()
        this.scroll.content.getComponent(UITransform).convertToNodeSpaceAR(wpos, itemPos)
        this.moveItem(itemPos, isLimit)
    }
    protected scrollFollowItem(itemView: SuperButton, isLimit: boolean = true) {
        let wpos = itemView.node.worldPosition;
        _temp.x = wpos.x;
        _temp.y = wpos.y;
        let children = this.scroll.content.children;
        let item = null;
        for (let index = 0; index < children.length; index++) {
            const element = children[index];
            let rect = element.getComponent(UITransform).getBoundingBoxToWorld()
            if (rect.contains(_temp)) {
                item = element;
                break;
            }
        }
        if (item) {
            this.moveItem(item.position, isLimit)
        }
    }

    protected moveItem(itemPos: Vec3, isLimit: boolean = true) {
        let sWidth = this.scroll.getComponent(UITransform).width
        let sHeight = this.scroll.getComponent(UITransform).height
        let hsw = sWidth / 2;
        let hsh = sHeight / 2;
        let contentPos = this.scroll.content.position;
        // let contentWidth = this.scroll.content.getComponent(UITransform).width;
        // let contentHeight = this.scroll.content.getComponent(UITransform).height;
        let disy = Math.abs(itemPos.y)
        let disx = Math.abs(itemPos.x)
        // LogUtils.log(' wpos ', wpos)
        // LogUtils.log(' disx ', disx)
        // LogUtils.log(' disy ', disy)
        // LogUtils.log(' itemPos ', itemPos)
        // LogUtils.log(' contentPos ', contentPos)
        // LogUtils.log(' contentWidth ', contentWidth)
        // LogUtils.log(' contentHeight ', contentHeight)
        // LogUtils.log(' sWidth ', sWidth)
        // LogUtils.log(' sHeight ', sHeight)
        if (this.scroll.vertical) {
            // let width = contentWidth - sWidth;
            // let pos = disx - hsw + itemSize.width / 2;
            // let percent = pos / width;
            if (isLimit) {
                let left = disy - sHeight;
                let offset = Math.abs(contentPos.y + hsh);

                if (left > offset) {
                    // if (contentPos.y - hsh + disy > sHeight) {
                    _tempVec2.set(0, disy - hsh)
                    this.scroll.scrollToOffset(_tempVec2, 0.2)
                    // this.scroll.scrollToPercentVertical(percent, 0.2, true)
                    // } else if (itemPos.y - contentPos.y - hsh < 0) {
                } else if (offset > disx) {
                    _tempVec2.set(0, disy - hsh)
                    this.scroll.scrollToOffset(_tempVec2, 0.2)
                    // this.scroll.scrollToPercentVertical(percent, 0.2, true)
                }
            } else {
                _tempVec2.set(0, disy - hsh)
                this.scroll.scrollToOffset(_tempVec2, 0.2)
            }



        } else if (this.scroll.horizontal) {
            // let width = contentWidth - sWidth;
            // let pos = disx - hsw + itemSize.width / 2;
            // let percent = pos / width;

            // let off = this.scroll.getScrollOffset()
            // LogUtils.log(' off ', off)
            if (isLimit) {
                let left = disx - sWidth;
                let offset = Math.abs(contentPos.x + hsw);
                // LogUtils.log(' left ', left, offset)
                if (left >= offset) {
                    // let disx = -(left - offset);
                    // LogUtils.log(' -left - hsw ', disx)
                    _tempVec2.set(disx - hsw, 0)
                    // LogUtils.log(' _tempVec2 ', _tempVec2)
                    this.scroll.scrollToOffset(_tempVec2, 0.2)
                    // this.scroll.scrollToPercentHorizontal(percent, 0.2, true)
                    // this.scroll.scrollToOffset(_tempVec2, 0.2)
                } else if (offset > disx) {
                    _tempVec2.set(disx - hsw, 0)
                    // LogUtils.log(' _tempVec2 ', _tempVec2)
                    this.scroll.scrollToOffset(_tempVec2, 0.2)
                    // this.scroll.scrollToPercentHorizontal(percent, 0.2, true)
                }
            } else {

                _tempVec2.set(disx - hsw, 0)
                this.scroll.scrollToOffset(_tempVec2, 0.2)
            }



        }
    }
}

